﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Project04
{
    class Object
    {
        private Vector3 position;
        public Vector3 Position
        {
            get { return position; }
            set { position = value; }
        }

        private VertexBuffer vertexBuffer;
        private List<VertexPositionColor> vertices;
        public List<VertexPositionColor> Vertices
        {
            get { return vertices; }
        }

        public Object(Vector3 position)
        {
            this.position = position;

            vertices = new List<VertexPositionColor>(); 
        }

        public void LoadVertecies(Color color)        
        {
            vertices.Add(new VertexPositionColor(new Vector3(Position.X + 5, 0, Position.Z - 5), color));
            vertices.Add(new VertexPositionColor(new Vector3(Position.X - 5, 0, Position.Z + 5), color));
            vertices.Add(new VertexPositionColor(new Vector3(Position.X + 5, 0, Position.Z + 5), color));
            vertices.Add(new VertexPositionColor(new Vector3(Position.X - 5, 0, Position.Z - 5), color));
        }

        public void LoadLineStrip(Vector3 pos, Color color)
        {
            vertices.Add(new VertexPositionColor(pos, color));
        }

        public void UpdatePosition(InputListener inputListener)
        {
            if (inputListener.isKeyPressed(Keys.A) && position.X > 200)
                position.X -= 10f;
            if (inputListener.isKeyPressed(Keys.D) && position.X < 400)
                position.X += 10f;
            if (inputListener.isKeyPressed(Keys.W) && position.Z > -100)
                position.Z -= 10f;
            if (inputListener.isKeyPressed(Keys.S) && position.Z < 100)
                position.Z += 10f;

            vertices[0] = new VertexPositionColor(new Vector3(Position.X + 5, 0, Position.Z - 5), Color.Red);
            vertices[1] = new VertexPositionColor(new Vector3(Position.X - 5, 0, Position.Z + 5), Color.Red);
            vertices[2] = new VertexPositionColor(new Vector3(Position.X + 5, 0, Position.Z + 5), Color.Red);
            vertices[3] = new VertexPositionColor(new Vector3(Position.X - 5, 0, Position.Z - 5), Color.Red);
        }

        public void Draw(GraphicsDevice graphicsDevice, BasicEffect basicEffect)
        {
            vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColor),
                vertices.Count, BufferUsage.WriteOnly);
            vertexBuffer.SetData<VertexPositionColor>(vertices.ToArray());

            graphicsDevice.SetVertexBuffer(vertexBuffer);

            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, 2);
                if (vertices.Count > 4)
                    graphicsDevice.DrawPrimitives(PrimitiveType.LineStrip, 3, vertices.Count - 3 - 1);
            }
        }
    }
}
